package com.lanswon.icc.admin.service;

import cn.hutool.core.util.ArrayUtil;
import com.lanswon.icc.admin.dao.SysElementMapper;
import com.lanswon.icc.admin.dao.SysRoleMapper;
import com.lanswon.icc.admin.dao.SysUserMapper;
import com.lanswon.icc.admin.dao.SysUserRoleMapper;
import com.lanswon.icc.admin.po.SysUser;
import com.lanswon.icc.admin.po.SysUserRole;
import com.lanswon.icc.admin.vo.*;
import com.lanswon.icc.common.core.dto.UserInfo;
import com.lanswon.icc.common.core.handle.ExceptionMsg;
import com.lanswon.icc.common.core.util.EntityUtil;
import com.lanswon.icc.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class UserService {


    private final SysUserMapper sysUserMapper;
    private final SysRoleMapper sysRoleMapper;
    private final SysElementMapper sysElementMapper;
    private final SysUserRoleMapper sysUserRoleMapper;



    public SysUser findSysUserByUsername(String username) {

        ExceptionMsg.checkedDataNull(username, "username", ExceptionMsg.check_param);

        SysUser sysUser = sysUserMapper.findByUsername(username);
        ExceptionMsg.checkedDataNull(sysUser, "sysUser", ExceptionMsg.check_result);

        return sysUser;


    }



    public UserInfo findByUsername(String username) {

        ExceptionMsg.checkedDataNull(username, "username", ExceptionMsg.check_param);

        SysUser sysUser = sysUserMapper.findByUsername(username);
        ExceptionMsg.checkedDataNull(sysUser, "sysUser", ExceptionMsg.check_result);

        Integer userId = sysUser.getUserId();
        List<String> permissions = sysElementMapper.queryPermissionByUserId(userId);

        List<String> roleCode = sysRoleMapper.queryRoleCodeByUserId(userId);

        com.lanswon.icc.common.core.po.admin.SysUser su = new com.lanswon.icc.common.core.po.admin.SysUser();

        BeanUtils.copyProperties(sysUser,su);

        return new UserInfo(su, permissions, roleCode);


    }

    public void createUser(CreateUpdateUserParams createUserVo) {
        ExceptionMsg.checkedDataNull(createUserVo, "createUserVo", ExceptionMsg.check_param);
        //TODO 验重 username
        SysUser sysUser = new SysUser();
        EntityUtil.setCreatAndUpdatInfo(sysUser);
        BeanUtils.copyProperties(createUserVo, sysUser);

        sysUserMapper.insertSelective(sysUser);

        List<Integer> roleIds = createUserVo.getRoleIds();

        List<SysUserRole> sysUserRoleList  = new ArrayList<>();
        if(roleIds != null && roleIds.size()>0){
            roleIds.forEach(roleId -> sysUserRoleList.add(new SysUserRole(sysUser.getUserId(),roleId)));
            sysUserRoleMapper.insertBatch(sysUserRoleList);
        }

    }



    public void deleteUser(DeleteUserParams deleteUserVo) {


    }

    public void updateUser(UpdateUserParams updateUserVo) {

    }
//
//    public Navigator<SysUser> querySysUserByPage(QueryUserByPageParams querySysUserByPageParams) {
//
//        List list = sysUserMapper.querySysUserByPage(querySysUserByPageParams);
//        List<SysUser> sysUserList = (List<SysUser>)(list.get(0));
//        List<Integer> countList = (List<Integer>) (list.get(1));
//        Integer totalCount = countList.get(0);
//        Pagination pagination = new Pagination(querySysUserByPageParams.getSize(),querySysUserByPageParams.getCurrent(),querySysUserByPageParams.getField(),querySysUserByPageParams.getSort().toLowerCase());
//        Navigator<SysUser> page = new Navigator<>(pagination,totalCount,sysUserList);
//
//        return page;
//    }

    public ViewUserVo currentUserInfo() {
        String username = SecurityUtils.getUser().getUsername();
        SysUser sysUser = sysUserMapper.findByUsername(username);
        ViewUserVo viewUserVo = new ViewUserVo();
        BeanUtils.copyProperties(sysUser,viewUserVo);
        Integer userId = sysUser.getUserId();
        List<String> permissions = sysElementMapper.queryPermissionByUserId(userId);
        List<Integer> roleIds = sysRoleMapper.queryRoleIdByUserId(userId);
        viewUserVo.setRoles(ArrayUtil.toArray(roleIds, Integer.class));
        viewUserVo.setPermissions(ArrayUtil.toArray(permissions, String.class));

        return viewUserVo;

    }

    public UserVO getUserVoById(Integer id) {
        return sysUserMapper.getUserVoByUserId(id);
    }



}
